#include "../include/linux/e820.h"
#include "../include/linux/kernel.h"
#include "../include/linux/bootparam.h"
#include "../include/linux/memblock.h"

struct e820_table e820_table;

void e820_memory_setup(void)
{
    struct boot_params* boot_params = (struct boot_params*)0x1100;
    for (int  i = 0; i < boot_params->boot_e820_entry_nr; i++)
    {
        struct boot_e820_entry* entry = &(boot_params->e820_table[i]);
        struct e820_entry* e820_e = &(e820_table.entries[i]);

        e820_e->type = entry->type;
        e820_e->addr.low = entry->addr.low;
        e820_e->addr.high = entry->addr.high;
        e820_e->size.low = entry->size.low;
        e820_e->size.high = entry->size.high;
        e820_table.nr_entries++;
    }
    
}

void e820_print_table(void)
{
    for (int  i = 0; i < e820_table.nr_entries; i++)
    {
        struct e820_entry* entry = &(e820_table.entries[i]);
        printk("from: 0x%08x%08x, size:0x%08x%08x, type:%d\n",
            entry->addr.high, entry->addr.low, 
            entry->size.high, entry->size.low, entry->type);
    }
    
}

void e820_memblock_setup(void)
{
    for (int i = 0; i < e820_table.nr_entries; i++)
    {
        struct e820_entry* entry = &e820_table.entries[i];
        if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
        {
            continue;
        }
        memblock_add(entry->addr.low, entry->size.low);
        
    }
}